<?php

include "../lib/start.php";

check_session();
check_permission(ADM_PERM);

/*
 * controllo per la presenza del nuovo anno
 */
$count = 0;
$sel_year = "SELECT COUNT(*) FROM anni WHERE data_inizio > NOW()";
try{
	$count = $db->executeCount($sel_year);
} catch (MySQLException $ex){
	$ex->redirect();
}

/*
 * ultimo anno in archivio: controlli per evitare duplicazioni
 */
$sel_anno = "SELECT * FROM anni ORDER BY id_anno DESC LIMIT 1";
$res_anno = $db->executeQuery($sel_anno);
$year = new AnnoScolastico($res_anno->fetch_assoc());
$anno = $year->get_ID();
$quadrimestre = 1;
if((date("Ymd")) > format_date($year->get_fine_quadrimestre(), IT_DATE_STYLE, SQL_DATE_STYLE, ""))
	$quadrimestre = 2;

$sel_cdc = "SELECT COUNT(*) FROM cdc WHERE id_anno = $anno";
$exist_cdc = $db->executeCount($sel_cdc);

$sel_reg = "SELECT COUNT(*) FROM reg_classi WHERE id_anno = $anno";
$exist_reg = $db->executeCount($sel_reg);

$sel_sch = "SELECT COUNT(*) FROM orario WHERE anno = $anno";
$exist_sch = $db->executeCount($sel_sch);

$check_data = "SELECT COUNT(*) FROM scrutini WHERE anno = $anno AND quadrimestre = $quadrimestre";
$count_data = $db->executeCount($check_data);

include "index.html.php";

?>